Server-Side WebAssembly by Danilo Chiarlone

Server-Side WebAssembly by Danilo Chiarlone

Author:Danilo Chiarlone [Danilo Chiarlone]
Language: eng
Format: epub
ISBN: 9781633436206
Publisher: Manning Publications Co.
Published: 2025-08-05T00:00:00+00:00


Overall, the full code can be seen in listing 5.9.

Listing 5.9 lib.rs file from smartcms_ml_guest

#[allow(warnings)] mod bindings; use tokenizers::Tokenizer; use rand::prelude::*; use ndarray::Array; use std::fs; use bindings::{Guest, wasi::nn::{ graph::{Graph, load, ExecutionTarget, GraphEncoding}, tensor::{Tensor, TensorType}, }}; struct Component; impl Guest for Component { fn storygen() -> String { let model_data = fs::read("fixture [CA]/modified_model.onnx").unwrap(); #A let graph = load(&[model_data], [CA]GraphEncoding::Onnx, ExecutionTarget [CA]::Cpu).unwrap(); #A let exec_context = Graph::init_execution_ [CA]context(&graph).unwrap(); #A let tokenizer = [CA]Tokenizer::from_file("fixture/ [CA]tokenizer.json").unwrap(); #B let prompt = "Once upon a time"; #C let encoding = tokenizer. [CA]encode(prompt, true).unwrap(); #C let mut input_ids = [CA]encoding.get_ids().to_vec(); #C let mut rng = thread_rng(); #D let eos_token_id = tokenizer.token_ [CA]to_id("<EOS>").unwrap_or(0); #D for _ in 0..100 { #E let sequence_length = input_ids.len(); #F let dimensions = vec! [CA][1, sequence_length as u32]; #F let input_f32: Vec<f32> = input_ [CA]ids.iter().map(|&id| id as f32).collect(); #F let mut input_data = Vec:: [CA]with_capacity(input_f32.len() * 4); #F for &val in &input_f32 { #F input_data.extend_from_slice [CA](&val.to_ne_bytes()); #F } #F let tensor = Tensor::new [CA](&dimensions, TensorType::Fp32, &input_data); #F exec_context.set_input [CA]("input_ids", tensor).unwrap(); #F exec_context.compute().unwrap(); #G let output_data = exec_context. [CA]get_output("logits").unwrap().data(); #H let output_f32 = bytes_to_f32_vec [CA](&output_data); #H let vocab_size = output_f32. [CA]len() / sequence_length; #I let start = (sequence_length - 1) [CA]* vocab_size; #I let end = sequence_length * vocab_size; #I let last_token_logits = [CA]&output_f32[start..end]; #I let probabilities = softmax [CA](last_token_logits); #J let dist = rand::distributions:: [CA]WeightedIndex::new(&probabilities).unwrap(); #K let next_token = dist.sample(&mut rng); #K input_ids.push(next_token as u32); #K let generated_text = [CA]tokenizer.decode(&input_ids, true).unwrap(); #L if generated_text.ends_with("

") { #M break; #M } #M if next_token as u32 == eos_token_id { #M break; #M } #M } tokenizer.decode(&input_ids, true) [CA].unwrap().trim().to_string() #N } } pub fn bytes_to_f32_vec(data: &[u8]) -> Vec<f32> { #O data.chunks_exact(4) .map(|c| f32::from_le_bytes(c.try_into().unwrap())) .collect() } fn softmax(logits: &[f32]) -> Vec<f32> { #P let logits_array = Array::from_vec(logits.to_vec()); let exp_logits = logits_array.mapv(f32::exp); let sum_exp = exp_logits.sum(); (exp_logits / sum_exp).to_vec() } bindings::export!(Component with_types_in bindings);

#A Loading the ONNX model and setting up the graph and execution context

#B Loading the tokenizer from the provided file

#C Defining the prompt and encoding it into token IDs

#D Initializing the random number generator and setting the EOS token

#E Looping up to 100 iterations to generate tokens

#F Preparing and setting the input tensor

#G Running the model inference

#H Retrieving and converting the model’s output

#I Extracting the logits for the last token in the sequence

#J Applying softmax to calculate token probabilities

#K Sampling the next token based on probabilities and appending it to the sequence

#L Decoding the generated text

#M Breaking the loop if a natural end to the story is detected

#N Returning the final generated story

#O The function to convert bytes to f32 values

#P The softmax function to compute the probability distribution



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Eco-friendly approach of bio-indigo synthesis and developing purification methods towards isolation of indigo from indirubin and bacterial fragments by Ramalingam Manivannan & Kaliyan Prabakaran & Young-A Son(231009)
Personalized inhaled bacteriophage therapy for treatment of multidrug-resistant Pseudomonas aeruginosa in cystic fibrosis by unknow(199582)
CONSORT 2025 statement: updated guideline for reporting randomized trials by unknow(102419)
Critical evaluation of the ProfiLER-02 study design and outcomes by Vivek Subbiah & Razelle Kurzrock(101954)
Cardiac gene therapy makes a comeback by Oliver J. Müller & Susanne Hille & Anca Kliesow Remes(101754)
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(74547)
Unveiling the design rules for tunable emission in graphene quantum dots: A high-throughput TDDFT and machine learning perspective by Şener Özönder & Mustafa Coşkun Özdemir & Caner Ünlü(50972)
A yeast-based oral therapeutic delivers immune checkpoint inhibitors to reduce intestinal tumor burden by unknow(40341)
Covalent hitchhikers guide proteins to the nucleus by Alexander F. Russell & Madeline F. Currie & Champak Chatterjee(40265)
Meet the Authors: Christopher R. Mansfield and Emily R. Derbyshire by Christopher R. Mansfield & Emily R. Derbyshire(40169)
Alkaline-earth metals promote propane dehydrogenation with carbon dioxide through geometric effects: Altering the reaction pathway by unknow(32792)
Induced iron vacancies boosting FeOOH loaded on sustainable Fenton-like collagen fiber membrane for efficient removal of emerging contaminants by unknow(32588)
Efficient electric-field-assisted photochemical conversion of methane to n-propanol exclusively over penetrated TiO2Ti hollow fibers by Guanghui Feng(32506)
Bi2SiO5 nanosheets as piezo-photocatalyst for efficient degradation of 2,4-Dichlorophenol by Hangyu Shi & Yifu Li & Lishan Zhang & Guoguan Liu & Qian Zhang & Xuan Ru & Shan Zhong(32449)
A novel NDIPTA organic heterojunction photocatalyst with built-in electric field for efficient hydrogen production by Jiahui Yang & Baojun Ma & Yongfa Zhu(32419)
Enhanced conversion of methane to liquid-phase oxygenates via hollow ferrite nanotube@horseradish peroxidase based photoenzymatic catalysis by Jun Duan & Shiying Fan & Xinyong Li & Shaomin Liu(32387)
Ordered macroporous superstructure of defective carbon adorned with tiny cobalt sulfide for selective electrocatalytic hydrogenation of cinnamaldehyde by Xiao-Shi Yuan & Sheng-Hua Zhou & San-Mei Wang & Wenbo Wei & Xiaofang Li & Xin-Tao Wu & Qi-Long Zhu(32305)
What's Done in Darkness by Kayla Perrin(27217)
Topological analysis of non-conjugated ethylene oxide cored dendrimers decorated with tetraphenylethylene: Insights from degree-based descriptors using the polynomial approach by A Theertha Nair & D Antony Xavier & Annmaria Baby & S Akhila(26600)
Investigation of mechanical and self-healing properties of hydroxyl-terminated polybutadiene functionalized with 2-ureido-4-pyrimidinone by Mohsen Kazazi & Mehran Hayaty & Ali Mousaviazar(26521)